﻿using HandyControl.Controls;
using HandyControl.Tools.Extension;
using PlaceOrder_Client.ViewModels;
using PlaceOrder_Client.Views;
using PlaceOrder_Core.Helper;
using Prism.DryIoc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using static PlaceOrder_Client.ViewModels.LoadingDialogViewModel;

namespace PlaceOrder_Client.Helper
{
    public class DialogHelper
    {
        private static object mylock = new object();
        //public static Dialog ShowLoading(string title = "加载中", int showDurationMs = 3000,Action action=null)
        //{
        //    LoadingDialogView loadingDialogView=new LoadingDialogView();
        //    // 如果loadingDialogView为null，则创建一个新实例
        //    if (loadingDialogView == null)
        //    {
        //        loadingDialogView = new LoadingDialogView();
        //    }

        //    // 使用单例实例创建对话框
        //    lock(DialogHelper.mylock)
        //    {
        //        var dialog = Dialog.Show(loadingDialogView)
        //       .Initialize<LoadingDialogViewModel>(vm =>
        //       {
        //           vm.Title = title;
        //       });

        //        if (action != null)
        //        {
        //            action.Invoke();
        //        }

        //        // 延迟一定时间后关闭对话框
        //        Task.Delay(showDurationMs).ContinueWith(_ =>
        //        {
        //            PrismApplication.Current.Dispatcher.Invoke(() =>
        //            {
        //                dialog.Close();
        //            });
        //        });

        //        return dialog;
        //    }

        //}


        private static bool isDialogShowing = false;
        public static async Task<Dialog> ShowLoadingAsync(string title = "加载中", int maxDurationMs = 10000, Func<Task> action = null)
        {
            Dialog dialog = null;
            try
            {
                if (isDialogShowing)
                {
                    // 如果已经有加载对话框正在显示，则直接返回 null 或者给出适当的提示
                    Growl.WarningGlobal("有正在执行的交易，请等交易完成后返回");
                    return null;
                }

                isDialogShowing = true;
                bool IsActionCompleted = false;
                LoadingDialogView loadingDialogView = new LoadingDialogView();

                dialog = Dialog.Show(loadingDialogView);

                loadingDialogView.closeBtn.Click += (sender, e) =>
                {
                    dialog.Close();
                };

                var vm = new LoadingDialogViewModel();
                loadingDialogView.DataContext = vm;
                vm.Title = title;

                try
                {
                    if (action != null)
                    {
                        await action();
                        //action.Invoke();
                        IsActionCompleted = true; // 标记 action 已经执行完成
                    }
                }
                catch (Exception ex)
                {
                    // 处理 action 执行过程中可能发生的异常
                    IsActionCompleted = false;
                    // 可以在这里记录日志或者显示错误消息
                    AppLog.WriteError($"DialogHelper进度条异常1: {ex.Message}");
                }
                if(IsActionCompleted==true)
                {
                    dialog.Close();
                    isDialogShowing = false; // 加载对话框关闭时，将标志设置为 false
                }
                // 等待指定的最大持续时间
                await Task.Delay(maxDurationMs);

                // 检查 action 是否已经执行完成
                if (!IsActionCompleted)
                {
                    // action 没有执行完成，关闭对话框
                    PrismApplication.Current.Dispatcher.Invoke(() =>
                    {
                        vm.Title = "异常";
                        dialog.Close();
                        isDialogShowing = false; // 加载对话框关闭时，将标志设置为 false
                    });
                    AppLog.WriteError($"DialogHelper委托任务未完成！");
                }
                else
                {
                    PrismApplication.Current.Dispatcher.Invoke(() =>
                    {
                        dialog.Close();
                        isDialogShowing = false; // 加载对话框关闭时，将标志设置为 false
                    });
                }

                return dialog;
            }
            catch (Exception ex)
            {
                // 处理整个方法可能发生的异常
                // 可以在这里记录日志或者显示错误消息
                PrismApplication.Current.Dispatcher.Invoke(() =>
                {
                    dialog?.Close();
                    isDialogShowing = false; // 加载对话框关闭时，将标志设置为 false
                });
                AppLog.WriteError($"DialogHelper进度条异常2: {ex.Message}");
                return null;
            }
            finally
            {
                // 确保最终重置 isDialogShowing
                isDialogShowing = false;
            }
        }




    }
}
